package ifmo.compiler.task21.lexer.tokens.identifiers;

import java.util.HashMap;
import java.util.Map;

/**
 * Factory for identifiers. Returns a general identifier or predefined word identifier
 * base on string value of identifier.
 *
 * @author byaminov
 */
public class IdentifierFactory {

    /* Map with predefined words */
    private static Map<String, Identifier> identifiersMap = new HashMap<String, Identifier>();
    static {
        put(new If());
        put(new Else());
        put(new Do());
        put(new While());
        put(new Goto());
        put(new Type());
        put(new Null());
        put(new True());
        put(new False());
        put(new New());
        put(new Int());
        put(new Float());
        put(new TypeRational());
        put(new Boolean());
        put(new Vector());
    }

    public static Identifier createToken(String value) {
        Identifier i = identifiersMap.get(value);
        if (i == null) {
            // not a predefined word
            i = new Identifier(value);
        }
        return i;
    }

    private static void put(Identifier i) {
        identifiersMap.put(i.getValue(), i);
    }

}
